<?php
/**
 * Created by PhpStorm.
 * User: wuz
 * Date: 2016/9/26
 * Time: 12:01
 */
namespace MyApp\Controllers\Front;

use MyApp\Controllers\Front\BaseController;
use MyApp\Models\Select;
use MyApp\Models\Student;
use Phalcon\Mvc\View;

/*
 *学生控制器
 * */
class StudentController extends BaseController
{

    /*
     * 学生列表
     * */
    public function listAction()
    {

        $student = new Student();

        //搜索
        if ($this->request->isPost()) {
            $search  = $this->request->getPost('search');
            $student = new Student();
            $data    = $student->select('*', [
                'studentName[~]' => [$search],
            ]);
            $this->view->setVar('data', $data);
        } else {
            $data = $student->select('*', 'order by time desc limit 10'); //最新十条
            $this->view->setVar('data', $data);
        }
        $this->view->disableLevel(View::LEVEL_MAIN_LAYOUT);

    }

    /*
     * 添加学生
     * */
    public function addAction()
    {

        if ($this->request->isPost()) {
            $studentName = $this->request->getPost('studentName', 'string');
            $gender      = $this->request->getPost('gender', 'string');
            $qq          = $this->request->getPost('qq', 'string');
            $tel         = $this->request->getPost('tel', 'string');
            $time        = time();
            $student     = new Student();
            $student->insert([
                'studentName' => $studentName, 'gender' => $gender, 'qq' => $qq, 'tel' => $tel, 'time' => $time,
            ]);}
        $this->view->disableLevel(View::LEVEL_MAIN_LAYOUT);

    }

    /*
     * 编辑学生
     * */
    public function editAction()
    {

        $id      = $this->request->get('id');
        $student = new Student();
        $data    = $student->select("*", ['id' => $id]);
        $this->view->setVar('list', $data);

        //编辑学生后提交
        if ($this->request->isPost()) {
            $studentName = $this->request->getPost('studentName', 'string');
            $gender      = $this->request->getPost('gender', 'string');
            $qq          = $this->request->getPost('qq', 'string');
            $tel         = $this->request->getPost('tel', 'string');
            $id          = $this->request->getPost('id', 'int');
            $time        = time();
            $student->update([
                'studentName' => $studentName, 'gender' => $gender, 'qq' => $qq, 'tel' => $tel, 'time' => $time,
            ], ['id' => $id]);
            $data = $student->select("*", ['id' => $id]);
            $this->view->setVar('list', $data); //将编辑完的结果重新显示到编辑界面
        }
        $this->view->disableLevel(View::LEVEL_MAIN_LAYOUT);
    }

    /*
     * 删除学生
     * */
    public function deleteAction()
    {
        if ($this->request->isPost()) {
            $student = new Student();
            $select  = new Select();
            $id      = $this->request->getPost('delete');
            $student->delete(
                ['id' => $id]
            );
            $select->delete(
                ['studentId' => $id]
            ); //同时删除该学生的选课记录
            $this->dispatcher->forward([
                "controller" => "student",
                "action"     => "list",
            ]);
        }
        $this->view->disableLevel(View::LEVEL_MAIN_LAYOUT);

    }

}
